package cn.drenal.capcap.service.impl;

import cn.drenal.capcap.entity.Activity;
import cn.drenal.capcap.entity.Artwork;
import cn.drenal.capcap.entity.vo.ActivityListVO;
import cn.drenal.capcap.entity.vo.forum.ForumActivityDetailVO;
import cn.drenal.capcap.mapper.ActivityMapper;
import cn.drenal.capcap.mapper.ArtworkMapper;
import cn.drenal.capcap.mapper.CategoryMapper;
import cn.drenal.capcap.service.ActivityService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * 活动服务实现类
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements ActivityService {

    private final CategoryMapper categoryMapper;
    private final ArtworkMapper artworkMapper;

    public ActivityServiceImpl(CategoryMapper categoryMapper, ArtworkMapper artworkMapper) {
        this.categoryMapper = categoryMapper;
        this.artworkMapper = artworkMapper;
    }

    @Override
    public Page<ActivityListVO> page(Integer current, Integer size, String name, Integer status) {
        Page<Activity> page = new Page<>(current, size);
        LambdaQueryWrapper<Activity> wrapper = new LambdaQueryWrapper<>();
        
        // 根据活动名称模糊查询
        wrapper.like(StringUtils.isNotBlank(name), Activity::getName, name);
        // 根据状态查询
        wrapper.eq(status != null, Activity::getStatus, status);
        // 按创建时间倒序排序
        wrapper.orderByDesc(Activity::getCreateTime);

        page = page(page, wrapper);

        Page<ActivityListVO> voPage = new Page<>();
        BeanUtils.copyProperties(page, voPage);
        voPage.setRecords(page.getRecords().stream().map(activity -> {
            ActivityListVO vo = new ActivityListVO();
            BeanUtils.copyProperties(activity, vo);
            vo.setCategoryName(categoryMapper.selectById(activity.getCategoryId()).getName());
            vo.setArtworkCount(Math.toIntExact(artworkMapper.selectCount(new LambdaQueryWrapper<Artwork>().eq(Artwork::getActivityId, activity.getId()))));
            return vo;
        }).toList());
        return voPage;
    }

    @Override
    public ForumActivityDetailVO incrementViewCountAndGet(Long id) {
        // 使用乐观锁方式更新访问量
        Activity activity = getById(id);
        if (activity != null) {
            // 使用update方法实现原子性的访问量递增
            update()
                .setSql("view_count = view_count + 1")
                .eq("id", id)
                .update();
            
            // 重新获取最新的活动信息
            activity = getById(id);
            ForumActivityDetailVO vo = new ForumActivityDetailVO();
            BeanUtils.copyProperties(activity, vo);
            vo.setCategoryName(categoryMapper.selectById(activity.getCategoryId()).getName());
            vo.setArtworkCount(Math.toIntExact(artworkMapper.selectCount(new LambdaQueryWrapper<Artwork>().eq(Artwork::getActivityId, activity.getId()))));
            return vo;
        }
        return null;
    }
}